//
//  HistoryEventDeatilController.m
//  Runone
//
//  Created by runone02 on 16/8/31.
//  Copyright © 2016年 runone. All rights reserved.
//

#import "HistoryEventDeatilController.h"
#import "RequestGinsengModel.h"
#import "RequestHelper.h"
#import "AppDelegate.h"
#import "MJRefresh.h"
#import "HistoryEventTopView.h"
#import "EventDeatilModel.h"
#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import "ROPointAnnotation.h"
#import "CustomAnnotationView.h"
#import "CustomCallOutView.h"

@interface HistoryEventDeatilController () <MAMapViewDelegate>

@property (nonatomic, strong)HistoryEventTopView *topView;
@property (nonatomic, strong) MAMapView *mapView;

@end

@implementation HistoryEventDeatilController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.title = [self eventType];
    
    [self loadMapView];
    [self configMapView];
    [self loadAnnotaion];
    
    [self configTopView];
    // Construction
    if ([self.title isEqualToString:@"施工事件"]) {
        [self loadConstructionData];
    }else{
        [self loadData];
    }
    
}

- (NSString *)eventType {
    NSString *string = nil;
    if ((self.CEModel.IncidentType == 1)) {
        string = [NSString stringWithFormat:@"事故事件"];
    }else {
        string = [NSString stringWithFormat:@"施工事件"];
    }
    return string;
}
- (void)configTopView {
    self.topView = [HistoryEventTopView  historyTopView];
    self.topView.frame = CGRectMake(0,210, self.view.bounds.size.width,118);
    [self.view addSubview:self.topView];
}

-(void)loadMapView
{
    [AMapServices sharedServices].apiKey = GETGAODEPAIKEY;
    _mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width,200)];
    _mapView.delegate = self;
    _mapView.showsCompass = NO;
    _mapView.showsScale= NO;
    [self.view addSubview:_mapView];
}
- (void)configMapView {
    MACoordinateSpan span = MACoordinateSpanMake(0.08, 0.08);
    CLLocationCoordinate2D centerCoordinate = CLLocationCoordinate2DMake(25.1902561, 113.212982);
    MACoordinateRegion region = MACoordinateRegionMake(centerCoordinate, span);
    _mapView.region = region;
}

- (void)loadAnnotaion {
    NSMutableArray *pointArray = [[NSMutableArray alloc] init];
    ROPointAnnotation *pointAnnotation = [[ROPointAnnotation alloc] init];
    float latitude = [self.CEModel.Latitude floatValue];
    float longitude = [self.CEModel.Longitude floatValue];
    pointAnnotation.coordinate=CLLocationCoordinate2DMake(latitude,longitude);
    pointAnnotation.currentEventModel = self.CEModel;
    [pointArray addObject:pointAnnotation];
    [_mapView addAnnotation:pointAnnotation];
    [_mapView showAnnotations:pointArray animated:YES];
}

- (void)loadData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAllHistoryEventModel=[[RequestGinsengModel alloc] init];
    GetAllHistoryEventModel.URL         = GetEventDataURL;
    GetAllHistoryEventModel.DataValue   = [self DataValueStr:self.CEModel.IncidentUID];
    GetAllHistoryEventModel.UpdatedTime = @"";
    GetAllHistoryEventModel.SystemCode  = @"GuangleHighway";
    GetAllHistoryEventModel.MethodName  = @"GetAccidentEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        EventDeatilModel *model = dataValueModel;
        [self.topView setData:model andCEModel:self.CEModel];
    };
    [requestHelper RequestGinsengModel:GetAllHistoryEventModel];
}

- (void)loadConstructionData {
    RequestHelper *requestHelper=[[RequestHelper alloc] init];
    RequestGinsengModel *GetAllHistoryEventModel=[[RequestGinsengModel alloc] init];
    GetAllHistoryEventModel.URL         = GetEventDataURL;
    GetAllHistoryEventModel.DataValue   = [self DataValueStr:self.CEModel.IncidentUID];
    GetAllHistoryEventModel.UpdatedTime = @"";
    GetAllHistoryEventModel.SystemCode  = @"GuangleHighway";
    GetAllHistoryEventModel.MethodName  = @"GetConstructionEventDetailInfo";
    requestHelper.block=^(RequestOutginseng *requestOutginseng,id dataValueModel){
        EventDeatilModel *model = dataValueModel;
        [self.topView setData:model andCEModel:self.CEModel];
    };
    [requestHelper RequestGinsengModel:GetAllHistoryEventModel];
    
    
}

#pragma mark -地图代理
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id <MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *pointReuseIndentifier = @"pointReuseIndentifier";
        CustomAnnotationView *annotationView = (CustomAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:pointReuseIndentifier];
        if (annotationView == nil)
        {
            annotationView = [[CustomAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:pointReuseIndentifier];
        }
        annotationView.canShowCallout= NO;       //设置气泡可以弹出，默认为NO
        
        ROPointAnnotation *anno = (ROPointAnnotation *)annotation;
        
        if ((anno.currentEventModel.IncidentType = 1)) {
            annotationView.image = [UIImage imageNamed:@"事故图标"];//事故图标
        }else {
            annotationView.image = [UIImage imageNamed:@"主线施工图标"];// 施工图标
        }
        return annotationView;
    }
    return nil;
}

-(NSString *)DataValueStr:(NSString *)IncidentUID
{
    NSString *valueStr =[NSString stringWithFormat:@"[{\"key\":\"EventUID\",\"value\":\"%@\"}]",IncidentUID];
    return valueStr;
}

@end
